﻿using System;
using System.Collections.Generic;
using System.Web.Script.Services;
using System.Web.Services;

/// <summary>
/// Summary description for Library
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class Library : System.Web.Services.WebService
{
    private static List<Book> _booksList;

    public Library()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
        Library._booksList = new List<Book>();
        InitializeBooks(Library._booksList);
    }

    private void InitializeBooks(List<Book> books)
    {
        books.Add(new Book()
        {
            Title = "JavaScript: The Good Parts",
            Author = "Douglas Crockford",
            ReleaseDate = "2008",
            Keywords = "JavaScript Programming"
        });
        books.Add(new Book()
        {
            Title = "The Little Book on CoffeeScript",
            Author = "Alex MacCaw",
            ReleaseDate = "2012",
            Keywords = "CoffeeScript Programming"
        });


        books.Add(new Book()
        {
            Title = "Scala for the Impatient",
            Author = "Cay S. Horstmann",
            ReleaseDate = "2012",
            Keywords = "Scala Programming"
        
        });

        books.Add(new Book()
        {
            Title = "American Psycho",
            Author = "Bret Easton Ellis",
            ReleaseDate = "1991",
            Keywords = "Novel Splatter"
        
        });

        books.Add(new Book
                      {
                          Title = "Eloquent JavaScript",
                          Author = "Marijn Haverbeke",
                          ReleaseDate = "2011",
                          Keywords = "JavaScript Programming"
                      });
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet = true,XmlSerializeString = false)]
    public List<Book> Books()
    {
        return _booksList;
    }
}

[Serializable]
public class Book
{
    private string _title;
    private string _author;
    private string _releaseDate;
    private string _keywords;

    public string Title
    {
        get { return _title; }
        set { _title = value; }
    }

    public string Author
    {
        get { return _author; }
        set { _author = value; }
    }

    public string ReleaseDate
    {
        get { return _releaseDate; }
        set { _releaseDate = value; }
    }

    public string Keywords
    {
        get { return _keywords; }
        set { _keywords = value; }
    }
}
